//
//  TelKSViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/30.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "TelKSViewController.h"
#import "UITextField+JKInputLimit.h"
#import "MapSelPlaceViewController.h"

#import "PayForViewController.h"
@interface TelKSViewController ()<MapSelPlaceViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *labPlace;
@property (weak, nonatomic) IBOutlet UILabel *labPlaceDet;
@property (weak, nonatomic) IBOutlet UIView *view1;

@property (weak, nonatomic) IBOutlet UITextField *textPlaceDet;
@property (weak, nonatomic) IBOutlet UITextField *textName;
@property (weak, nonatomic) IBOutlet UITextField *textPhone;
@property (nonatomic,copy)NSString *str;
@end

@implementation TelKSViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self showNav:@"呼叫开锁"];
    [self loadNum];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click1)];
    _view1.userInteractionEnabled = YES;
    [_view1 addGestureRecognizer:tap];
    
    _textName .jk_maxLength = 8;
    _textPhone.jk_maxLength = 11;
    
}

- (void)click1{
    MapSelPlaceViewController *map = [[MapSelPlaceViewController alloc] init];
    map.delegate = self;
    [self.navigationController pushViewController:map animated:YES];
}

-(void)sendText:(NSString *)text Province:(NSString *)proce City:(NSString *)city Area:(NSString *)area{
    
}

-(void)sendAddress:(NSString *)address Det:(NSString *)det{
    _labPlace.text = det;
    _labPlaceDet.text = address;
}

-(void)sendLat:(NSString *)lat sendLng:(NSString *)lng{
    
}

- (IBAction)clickTel:(id)sender {
    if ([_labPlace.text isEqualToString:@"请选择"]) {
        
        [LICGToast showWithText:@"请选择地址"];
        return;
    }else if (_textPlaceDet.text.length == 0){

        [LICGToast showWithText:@"请输入详细地址"];
        return;
    }else if (_textName.text.length == 0){

        [LICGToast showWithText:@"请输入联系人名字"];
        return;
    }else if ([self validateMobile:_textPhone.text] == 0){
        
        [LICGToast showWithText:@"电话格式不正确"];
        return;
    }
    
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    NSString *strs = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:strs];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    [parmas setObject:token forKey:@"verifiToken"];
    [parmas setObject:_merid forKey:@"merchantid"];
    [parmas setObject:_labPlace.text forKey:@"address"];
    [parmas setObject:_textPlaceDet.text forKey:@"detailedaddress"];
    [parmas setObject:_textName.text forKey:@"person"];
    [parmas setObject:_textPhone.text forKey:@"phone"];
    [parmas setObject:_orderName forKey:@"ordername"];
    
    NSLog(@"parmas:%@",parmas);
    NSString *url = [NSString stringWithFormat:@"%@/App/Shoppingcart/lockorderpay",urlHead];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            [self alerts];
        }else{
            [SVProgressHUD showErrorWithStatus:[responseObject objectForKey:@"mssage"]];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
    
}

- (void)alerts{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"呼叫成功，商家会尽快处理，与你联系，请保持通讯畅通" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action0 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    [alert addAction:action0];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}

//生成订单号
- (void)loadNum{
    NSString *url = [NSString stringWithFormat:@"%@/App/Shoppingcart/ordernumber",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",responseObject);
        _str = [responseObject objectForKey:@"result"];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
